﻿using System;
using EmbeddedSharp.Core;
using Zove.Runtime.Core;
using Zove.Scripting.Core.Functions;
using Zove.Runtime.Core.Zoom;

namespace EmbeddedSharp.Scripting.Interfacing.Mathematica
{
    public class MmaConvertNode : ContextFuncNode
    {
        public MmaConvertNode()
        {
            base.Value = "mmaConvert";
        }

        public override object Evaluate()
        {
            if (Parameters.Count < 1)
            {
                throw new ArgumentException(
                    "usage: mmConvert(expr);");
            }

            Node node = Parameters[0];
            if (node is FormulateNode)
            {
                string expr = node.Evaluate().ToString();
                ZoomLanguageNature languageNature = new ZoomLanguageNature();
                ExpressionParser p = languageNature.ExpressionParserFactory.CreateParser(expr, Context);
                node = p.Parse();
            }

            var translator
                = new MathematicaExpressionTranslator("");
            string result = translator.Translate(node);

            return result;
        }
    }
}